/**
 * Created by charlie on 17-12-27.
 */
import { Actions } from 'react-native-router-flux'
import {getTabel} from '../services/taiZhang'
import {Toast} from 'antd-mobile'

function showToast(msg) {
  Toast.fail(msg, 2)
}

export default {
    namespace:'taiZhang',
    state:{
        data:[],
        list:[],
        page: 1,
        perPage: 9,
        id:'',
        last:0,
        loading:true,
        tabNum:0,
        prjname:'',
        rowData:'',
        startDate: '',
        endDate:'',
    },

    effects:{
        *getTableList({payload},{put, select, call}){
            yield put({type:'getLoading'});
            let current = '';
            let TabNum = '';
            const { page, perPage,tabNum,prjname,rowData,startDate,endDate} = yield select(state => state.taiZhang)
            const {userId} = yield  select(state => state.login)
            if(payload === '' || payload === undefined){
                current = page;
                TabNum = tabNum;
            }else {
                if (payload.page ==='' || payload.page === undefined){
                    current = page;
                }else {
                    current = payload.page;
                }
                if (payload.tabNum ==='' || payload.tabNum === undefined){
                    TabNum = tabNum;
                }else {
                    TabNum = payload.tabNum;
                }
            }
            const { json, error } = yield call(getTabel, {pageNo: current, pageSize: perPage,userId:userId,
                "tbprojectinfo.prjname":prjname,handleResult:rowData,inspectDate:startDate,searchDate:endDate})

            if(error){
                yield put({type: 'cleanLoading'})
            }else{
              yield put({type: 'thisSuccess', payload: json.data})
            }
        },

        *getDetail({payload},{put,select,call}){
            yield put({type:'seeDetail',payload:payload})
        },

        *search({payload},{put,select,call}){
          yield put({type:'getTableList',payload:{page:1}})
        },

    },
    reducers:{
        thisSuccess(state,{payload}){
            let num = payload.pageNo;
            let last = payload.last;
            let list = payload.list.map(val => {
                let tmp = {
                    id: val.id,
                    tableData: [
                        val.corpName,
                        val.prjName,
                        val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
                        val.supervisor,
                        val.handleResult
                    ]
                };
                return tmp
            });
            return {...state, list: list,page:num,last:last,loading:false}
        },

        seeDetail(state,{payload}){
            Actions.push('taiZhangDetail');
            return {...state,id:payload}
        },
        getLoading(state,{payload}){
            return{...state,loading:true}
        },
        cleanLoading(state,{payload}){
            return{...state,loading:false}
        },
        getClean(state,{payload}){
            return {...state,loading:true,page:1,prjname:'',rowData:'',startDate:'',endDate:''}
        },
        changeText(state,{payload}){
            return {...state,prjname:payload}
        },
        selectRowData(state,{payload}){
            return {...state,rowData:payload}
        },
        setStartDate(state,{payload}){
            return {...state,startDate:payload}
        },
        setEndDate(state,{payload}){
            return {...state,endDate:payload}
        },
    }
}